/*
 * 请求相关,异步api同步封装
 */
 

import {HTTP_REQUEST_URL} from '../config.js' 

const request = {} 

/*
 * opts:请求参数 url,data,datatype,method, 
 * opts:{
	methods:// 请求方式
	url:    // 请求地址
	data:   // 请求参数 
 }
 * 
 * 
 * 
 */

request.globalRequest = (opts) => {   
	let contentType = "application/x-www-form-urlencoded"; 
	if (opts.type && opts.type == 2) {
	    contentType = "application/json";
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url:opts.url,
			method:opts.methods,
			data:opts.data,
			dataType:'json',
			timeout: 30000,
			header: {
			  "Content-Type": contentType,
			  'Cookie': uni.getStorageSync('cookiekey') 
			}, 
			success: (res) => {		  
				resolve(res);
			},
			fail: (err) => { 
				console.log(err.errMsg,err.errMsg.indexOf('request:fail abort statusCode:-1'))
				if(err.errMsg.indexOf('request:fail timeout')>=0){
					uni.showModal({
						// content:'网络请求失败，请稍后在试',opts.url
						content:'请求超时，重试',
						cancelText:'取消',
						confirmText:'确定',
						success(res) {
							if(res.confirm){
								
							}
						}
					})
				}else if(err.errMsg=='request:fail'){
					uni.showModal({
						// content:'网络请求失败，请稍后在试',opts.url
						content:'当前网络不佳，请检查网络连接后重试',
						cancelText:'取消',
						confirmText:'确定',
						success(res) {
							if(res.confirm){
								
							}
						}
					})
				}else if(err.errMsg.indexOf('request:fail abort statusCode:-1')>=0){
					uni.showModal({
						// content:'网络请求失败，请稍后在试',opts.url
						content:'当前网络不佳，请检查网络连接后重试',
						cancelText:'取消',
						confirmText:'确定',
						success(res) {
							if(res.confirm){
								
							}
						}
					})
				}else{
					uni.showModal({
						// content:'网络请求失败，请稍后在试',opts.url
						content:'网络异常',
						cancelText:'取消',
						confirmText:'刷新重试',
						success(res) {
							if(res.confirm){
								
							}
						}
					})
				}			 
				reject(err)
			}
		})
	})
}

request.vPost = (opts) => {  
	let contentType = "application/x-www-form-urlencoded";
	if (opts.type && opts.type == 2) {
	    contentType = "application/json";
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url:opts.url,
			method:'post',
			data:opts.data,
			dataType:'json',
			timeout: 30000,
			header: {
			  "Content-Type": contentType,
			  'Cookie': uni.getStorageSync('cookiekey') 
			}, 
			success: (res) => {				
				resolve(res);
			},
			fail: (err) => {	 
				uni.showModal({
					content:'网络请求失败，请稍后在试?',
					cancelText:'取消',
					confirmText:'刷新重试',
					success(res) {
						if(res.confirm){
							
						}
					}
				})
				 
				reject(err)
			}
		})
	})
}

request.vGet = (opts,type) => {  
	let contentType = "application/x-www-form-urlencoded";
	if (opts.type&&opts.type == 2) {
	    contentType = "application/json";
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url:opts.url,
			method:'get',
			data:opts.data,
			dataType:'json',
			timeout: 30000,
			header: {
			  "Content-Type": contentType,
			  'Cookie': uni.getStorageSync('cookiekey') 
			}, 
			success: (res) => {				
				resolve(res);
			},
			fail: (err) => {	 
				uni.showModal({
					content:'网络请求失败，请稍后在试',
					cancelText:'取消',
					confirmText:'刷新重试',
					success(res) {
						if(res.confirm){
							
						}
					}
				})
				 
				reject(err)
			}
		})
	})
}

/*
 * opts:请求参数 url,filePath,name,formData, 
 * opts:{
	filePath(string):// 本地资源路径
	name(string):    // 上传文件键名
	formData(object):   // 其他参数  
 }
 * 
 * 
 * 
 */
request.uploadFile = (opts) => {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url:opts.url,
			filePath:opts.filePath,
			name:opts.name,
			header: {
			  "Content-Type": "multipart/form-data",
			  'Cookie': uni.getStorageSync('cookieKey') 
			},
			formData:opts.data,
			success: (res) => {
				resolve(res);
			},
			fail: (err) => {				 
				uni.showModal({
					content:'网络请求失败，请稍后在试',
					cancelText:'取消',
					confirmText:'刷新重试',
					success(res) {
						if(res.confirm){
							
						}
					}
				})
				 
				reject(err)
			}
		})
	})
}


export default request

 













